<?php

namespace common;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * CMS助手类
 *
 * @package common
 */
class CMS
{

    const VERSION = '0.0.1';

    /**
     * 查找文件并返回合并后的配置
     *
     * @param string $name
     * @param array  $aliasList
     * @return array
     */
    public static function globModuleConfig($name, $aliasList = ['common'])
    {
        $config = [];

        foreach ($aliasList as $alias)
        {
            $globPath = "@{$alias}/modules/*/config/{$name}.php";

            $files = glob(Yii::getAlias($globPath));
            foreach ($files as $file)
            {
                $config = ArrayHelper::merge(
                    require $file,
                    $config
                );
            }
        }

        return $config;
    }

    /**
     * 签名函数
     *
     * @param string $apiName
     * @param string $uri
     * @param array  $params
     * @param string $secret
     * @return string
     */
    public static function generateSign($apiName, $uri, array $params, $secret)
    {
        ksort($params);

        $sourceStr = $apiName . '&' . rawurlencode($uri) . '&' . urlencode(http_build_query($params));

        return md5($sourceStr . '&' . $secret);
    }
}
